package com.example.juc;


import jmh.JMHTest;
import org.junit.Test;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.*;
import java.util.concurrent.*;
import java.util.stream.IntStream;

/**
 * juc 容器速度测试
 *
 * @author csp 2022-10-09
 */
@BenchmarkMode(Mode.AverageTime)
@Warmup(iterations = 1, time = 5)
@Measurement(iterations = 5, time = 5)
@Threads(1)
@Fork(1)
@State(value = Scope.Benchmark)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public class JUCTest {



    @Benchmark
    public void copyOnWriteArrayList() {
        CopyOnWriteArrayList<Object> list = new CopyOnWriteArrayList<>();
        IntStream.range(0, 100000)
                .parallel()
                .forEach(e -> list.add(UUID.randomUUID().toString()));
        if (list.size() < 100000) {
            System.out.println("异常：" + list.size());
        }
    }

    @Benchmark
    public void concurrentLinkedQueue(){
        ConcurrentLinkedQueue<Object> queue = new ConcurrentLinkedQueue<>();
        IntStream.range(0, 100000)
                .parallel()
                .forEach(e -> queue.add(UUID.randomUUID().toString()));
        if (queue.size() < 100000) {
            System.out.println("异常：" + queue.size());
        }
    }

    @Benchmark
    public void concurrentLinkedDueue(){
        ConcurrentLinkedDeque<Object> queue = new ConcurrentLinkedDeque<>();
        IntStream.range(0, 100000)
                .parallel()
                .forEach(e -> queue.add(UUID.randomUUID().toString()));
        if (queue.size() < 100000) {
            System.out.println("异常：" + queue.size());
        }
    }

    @Benchmark
    public void arraylist(){

        ArrayList<Object> list = new ArrayList<>();
        IntStream.range(0, 100000)
                .parallel()
                .forEach(e -> list.add(UUID.randomUUID().toString()));
        if (list.size() < 100000) {
            System.out.println("异常：" + list.size());
        }
    }

    @Benchmark
    public void linkedlist(){

        List<Object> list = new LinkedList<>();
        IntStream.range(0, 100000)
                .parallel()
                .forEach(e -> list.add(UUID.randomUUID().toString()));
        if (list.size() < 100000) {
            System.out.println("异常：" + list.size());
        }
    }

    @Benchmark
    public void arrayblockingqueue(){
        ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue<>(100000);
        IntStream.range(0, 100000)
                .parallel()
                .forEach(e -> arrayBlockingQueue.add(UUID.randomUUID().toString()));
        if (arrayBlockingQueue.size() < 100000) {
            System.out.println("异常：" + arrayBlockingQueue.size());
        }
    }

    public static void main(String[] args) throws RunnerException {
        Options options = new OptionsBuilder()
                .include(JUCTest.class.getSimpleName())
                .result("result.json")
                .build();
        new Runner(options).run();
    }



}
